
//处理返回的数据
function setList(resDate,dateList){
    var tableDate = [];

    for(var j=0;j<resDate.length;j++){
        var thisDate = resDate[j];
        var monthCount = (thisDate.monthCount!=null?thisDate.monthCount:0);//月销量
        //亚马逊库存数量
        var fulfillableQuantity = (thisDate.fulfillableQuantity!=null?thisDate.fulfillableQuantity:0);
        //在途库存数量
        var wayNum = (thisDate.wayNum!=null?thisDate.wayNum:0);
        //仓储库存数量
        var inboundWorkingQuantity = (thisDate.inboundWorkingQuantity!=null?thisDate.inboundWorkingQuantity:0);

        //亚马逊系数
        var amazonCount = (parseInt((fulfillableQuantity / monthCount) * 10) / 10).toFixed(1);
        //在途系数
        var wayCount = (parseInt(((fulfillableQuantity + wayNum) / monthCount) * 10) / 10).toFixed(1);
        //亚马逊系数
        var boundCount = (parseInt(((fulfillableQuantity + wayNum + inboundWorkingQuantity) / monthCount) * 10) / 10).toFixed(1);

        thisDate.amazonCount = amazonCount;
        thisDate.wayCount = wayCount;
        thisDate.boundCount = boundCount;

        var info = thisDate.Info;
        //console.log('info：',info);
        if(info!=null){
            var jsonInfo = eval('([' + info + '])');
            console.log('jsonInfo：',jsonInfo);

            var newthisDate1 = thisDate;
            var newthisDate2 = thisDate;
            var newthisDate3 = thisDate;
            var newthisDate4 = thisDate;
            newthisDate1.itemStr = '排名';
            console.log('newthisDate：',newthisDate1);
            console.log('thisDate：',thisDate);
            var newthisDate1 = eachSetDateInfo(dateList,newthisDate1,jsonInfo,'0');

            tableDate.push(newthisDate1);

            newthisDate2.itemStr = '售价';
            newthisDate2 = eachSetDateInfo(dateList,newthisDate2,jsonInfo,'1');
            //console.log('thisDate：',newthisDate2);
            tableDate.push(newthisDate2);

            //newthisDate3.itemStr = '数量';
            //thisDate = eachSetDateInfo(dateList,newthisDate3,jsonInfo,'2');
            //console.log('thisDate：',newthisDate3);
            //tableDate.push(newthisDate3);
            //thisDate.itemStr = '访问量';
            //thisDate = eachSetDateInfo(dateList,thisDate,jsonInfo,'3');
            //console.log('thisDate：',thisDate);
            //tableDate.push(thisDate);

        }else {
            thisDate.itemStr = '排名';
            tableDate.push(thisDate);
            thisDate.itemStr = '售价';
            tableDate.push(thisDate);
            thisDate.itemStr = '数量';
            tableDate.push(thisDate);
            thisDate.itemStr = '访问量';
            tableDate.push(thisDate);
        }
    }
    return tableDate;
}

function eachSetDateInfo(dateList,thisDate,jsonInfo,z){
    for(var i=0;i<dateList.length;i++){

        var countNum = "";
        var thisDateTime = dateList[i].dateTime;
        var thisDateNum = dateList[i].dateNum;
        var flag = 0;
        for(var c=0; c<jsonInfo.length; c++ ){
            if(z=='0'){
                countNum = jsonInfo[c].rank;
            }else if(z=='1'){
                countNum = jsonInfo[c].price;
            }else if(z=='2'){
                countNum = jsonInfo[c].count;
            }else {
                countNum = jsonInfo[c].visit;
            }
            if(thisDateTime == jsonInfo[c].date){
                flag++;

                thisDate[thisDateNum] = countNum;
            }
        }
        //if(flag==0){
        //thisDate[thisDateNum] = "";
        //}
    }
    return thisDate;

}

//获取过去dayNum天的日期
function getAgoDay(nowTime,dayNum) {
    var oldDate = new Date(nowTime);
    oldDate.setDate(nowTime.getDate() - dayNum);
    var yearNum = oldDate.getFullYear();
    var monthNum = oldDate.getMonth() + 1;
    var dayNum = oldDate.getDate();
    var agoDay = yearNum + '-' + (monthNum<10?"0"+monthNum:monthNum) + '-' + (dayNum<10?"0"+dayNum:dayNum);
    return agoDay;
}


//行合并 start
function execRadioRows(childFilterArr, ckChildFilterArr, flag) {
    //获取td的个数和种类
    var chChildFilterTextObj = {};
    var chText = [];
    var chIndex = [];

    for (var i = 0; i < ckChildFilterArr.length; i++) {
        var chChildText = flag ? ckChildFilterArr[i].innerHTML : ckChildFilterArr[i].textContent;
        if (chChildFilterTextObj[chChildText] == undefined) {
            chChildFilterTextObj[chChildText] = 1;
            chText.push(chChildText);
        } else {
            var num = chChildFilterTextObj[chChildText];
            chChildFilterTextObj[chChildText] = num * 1 + 1;
        }
    }
    for (var i = 0; i < chText.length; i++) {
        var chNum = 0;
        for (var j = 0; j < ckChildFilterArr.length; j++) {
            var chChildText = flag ? ckChildFilterArr[j].innerHTML : ckChildFilterArr[j].textContent;
            if (chText[i] == chChildText) {
                chNum = chNum + 1
            }
        }
        chIndex.push(chNum);
    }
    var newIndex = [];
    for (var i = 0; i < chIndex.length; i++) {
        if (i == 0) {
            newIndex.push(0);
        } else {
            var newNum = 0;
            for (var j = 0; j < chIndex.length; j++) {
                if (j < i) {
                    newNum = newNum + chIndex[j];
                }
            }
            newIndex.push(newNum);
        }
    }
    chIndex = newIndex;

    for (var j = 0; j < childFilterArr.length; j++) {
        var findFlag = false;
        for (var k = 0; k < chIndex.length; k++) {
            if (j == chIndex[k]) {
                findFlag = true;
                if (chIndex[k + 1] != null) {
                    childFilterArr[j].setAttribute("rowspan", chIndex[k + 1] - j);
                    $(childFilterArr[j]).find("div.rowspan").parent("div.layui-table-cell").addClass("rowspanParent");
                    $(childFilterArr[j]).find("div.layui-table-cell")[0].style.height = (chIndex[k + 1] - j) * 38 - 10 + "px";

                } else {
                    childFilterArr[j].setAttribute("rowspan", childFilterArr.length - j);
                    $(childFilterArr[j]).find("div.rowspan").parent("div.layui-table-cell").addClass("rowspanParent");
                    $(childFilterArr[j]).find("div.layui-table-cell")[0].style.height = (childFilterArr.length - j) * 38 - 10 + "px";
                }
            }
        }
        if (findFlag == false) {
            childFilterArr[j].style.display = "none";
        }
    }
}
function execRowspan(fieldName, index, flag, ckRows) {
    var fixedNode = $(".layui-table-body")[index];
    var child = $(fixedNode).find("td");
    var childFilterArr = [];
    for (var j = 0; j < child.length; j++) {
        child[j].getAttribute('data-field')
        if (child[j].getAttribute("data-field") == fieldName) {
            childFilterArr.push(child[j]);
        }
    }

    var ckChildFilterArr = [];
    if (fieldName == "0") {
        for (var j = 0; j < child.length; j++) {
            child[j].getAttribute('data-field')
            if (child[j].getAttribute("data-field") == ckRows) {
                ckChildFilterArr.push(child[j]);
            }
        }
        execRadioRows(childFilterArr, ckChildFilterArr, flag);
        return;
    }

    //获取td的个数和种类
    var childFilterTextObj = {};
    for (var i = 0; i < childFilterArr.length; i++) {
        var childText = flag ? childFilterArr[i].innerHTML : childFilterArr[i].textContent;
        if (childFilterTextObj[childText] == undefined) {
            childFilterTextObj[childText] = 1;
        } else {
            var num = childFilterTextObj[childText];
            childFilterTextObj[childText] = num * 1 + 1;
        }
    }
    var canRowspan = true;
    var maxNum;//以前列单元格为基础获取的最大合并数
    var finalNextIndex;//获取其下第一个不合并单元格的index
    var finalNextKey;//获取其下第一个不合并单元格的值
    for (var i = 0; i < childFilterArr.length; i++) {
        (maxNum > 9000 || !maxNum) && (maxNum = $(childFilterArr[i]).prev().attr("rowspan") && fieldName != "8" ? $(childFilterArr[i]).prev().attr("rowspan") : 9999);
        var key = flag ? childFilterArr[i].innerHTML : childFilterArr[i].textContent;//获取下一个单元格的值
        var nextIndex = i + 1;
        var tdNum = childFilterTextObj[key];
        var curNum = maxNum < tdNum ? maxNum : tdNum;
        if (canRowspan) {
            for (var j = 1; j <= curNum && (i + j < childFilterArr.length);) {//循环获取最终合并数及finalNext的index和key
                finalNextKey = flag ? childFilterArr[i + j].innerHTML : childFilterArr[i + j].textContent;
                finalNextIndex = i + j;
                if ((key != finalNextKey && curNum > 1) || maxNum == j) {
                    canRowspan = true;
                    curNum = j;
                    break;
                }
                j++;
                if ((i + j) == childFilterArr.length) {
                    finalNextKey = undefined;
                    finalNextIndex = i + j;
                    break;
                }
            }
            childFilterArr[i].setAttribute("rowspan", curNum);
            if ($(childFilterArr[i]).find("div.rowspan").length > 0) {//设置td内的div.rowspan高度适应合并后的高度
                $(childFilterArr[i]).find("div.rowspan").parent("div.layui-table-cell").addClass("rowspanParent");
                $(childFilterArr[i]).find("div.layui-table-cell")[0].style.height = curNum * 38 - 10 + "px";
            }
            canRowspan = false;
        } else {
            childFilterArr[i].style.display = "none";
        }
        if (--childFilterTextObj[key] == 0 | --maxNum == 0 | --curNum == 0 | (finalNextKey != undefined && nextIndex == finalNextIndex)) {
            canRowspan = true;
        }
    }
}
function layuiRowspan(fieldNameTmp, index, flag, ckRows) {
    var fieldName = [];
    if (typeof fieldNameTmp == "string") {
        fieldName.push(fieldNameTmp);
    } else {
        fieldName = fieldName.concat(fieldNameTmp);
    }
    for (var i = 0; i < fieldName.length; i++) {
        execRowspan(fieldName[i], index, flag, ckRows);
    }
}
//行合并 end


